
export const debounce = (fn, delay) => {
  let timer = null;
  return function(){
    if(timer){
      clearTimeout(timer)
    }
    timer = setTimeout(fn, delay)
  }
}
export const throttle = (fn, delay) => {
  let valid = true
  return function() {
      if(!valid){
          //休息时间 暂不接客
          return false 
      }
      // 工作时间，执行函数并且在间隔期内把状态位设为无效
      valid = false
      setTimeout(() => {
          fn()
          valid = true;
      }, delay)
  }
}

export const reg_phone = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/

export const checkPhoneNumber = (phone) => {
  return reg_phone.test(phone)
}

export const isFloat = n => {
  return /^-?\d*\.\d+$/.test(n)
}

export const sliceStrWrap = (params, num) => {  // 图例文本过长换行
  let tmp = '', tip = '', len = params.length;
  if(len > num){
    let s = Math.ceil(len/num)
    for(let i = 1; i <= s; i++){
      if(i < s){
        tmp += params.slice(i*num - num, i * num) + '\n'
      }else if(i === s){
        tip = tmp + params.slice((s - 1) * num, len)
      }
    }
    return tip;
  }else{
    tip = params;
    return tip;
  }
}

export const getTimeRangeData = (st, et) => {
    let dateList = [],
        beginTime = new Date(st).getTime(),
        endTime = new Date(et).getTime(),
        arr = [],
        current = beginTime,
        tmpYear,
        tmpMonth

    while(current <= endTime){
        let d = new Date(current)
        arr.push(`${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()}`)

        if(dateList.length != 0 && tmpYear == d.getFullYear() && tmpMonth == d.getMonth() + 1){
            dateList.forEach(v => {
                if(v.year == d.getFullYear() && v.month == d.getMonth() + 1){
                    v.dayList.push({
                        id: d.getDate(),
                        date: d.getDate(),
                        day: d.getDay()
                    })
                }
            })
        }else{
            dateList.push({
                id: dateList.length + 1,
                year: d.getFullYear(),
                month: d.getMonth() + 1,
                dayList: [
                    {
                        id: d.getDate(),
                        date: d.getDate(),
                        day: d.getDay()
                    }
                ]
            })
        }

        tmpYear = d.getFullYear()
        tmpMonth = d.getMonth() + 1

        current += 1000 * 60 * 60 * 24
    }

    return dateList;
}

export const replaceStr = str => str.toLowerCase().replace(/\b(\w)|\s(\w)/g, m => m.toUpperCase())
//
let format = (value) => {
    return value >= 10 ? value : '0' + value
};
//本周日期
export const makeWeekDate = () =>{
    var new_Date = new Date()
    var timesStamp = new_Date.getTime();
    var currenDay = new_Date.getDay();
    var dates = [];
    for(var i = 0; i < 7; i++) {
        let newArr = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString().replace(/[年月]/g, '-').replace(/[日上下午]/g, '').split('/')
        dates.push(newArr[0]+'-'+format(newArr[1])+'-'+format(newArr[2]));
        //new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString().replace(/[年月]/g, '-').replace(/[日上下午]/g, '')
    }
    return dates
}
//本月开始结束日期
export const makeMonthDate=function(){
    var new_Date = new Date()
    var y = new_Date.getFullYear()
    var m = new_Date.getMonth() + 1
        let d = 30;
    m = Number(m);
    switch (m) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            d = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            d = 30;
            break;
        case 2:
            d = 28;
            if ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) {
                d = 29;
            }
            break;
    }
    return {s:y+'-'+format(m)+'-01',e:y+'-'+format(m)+'-'+format(d)};
}
//today
export const makeTodayDate=function(){
    var new_Date = new Date()
    var y = new_Date.getFullYear()
    var m = new_Date.getMonth() + 1
    var d = new_Date.getDate()
    return y+'-'+format(m)+'-'+format(d)
}

export const getCurrWeekend = () => {
    let d = new Date();
    let year = d.getFullYear();
    let month = d.getMonth() + 1;
    let time = year + `/` + month + `/` + `01`;
    let startDate = new Date(time)
    let day = parseInt(startDate.getDay()) == 0 ? 7 : parseInt(startDate.getDay()); // 获取本月开始日期
    console.log(day)
    // 判断当月有几周
    let days = new Date(year, month, 0).getDate();
    let y = days % 7;
    if (7 - day >= y) {
        var zhou = parseInt(days / 7) + 1;
    } else {
        var zhou = parseInt(days / 7) + 2;
    }
    let endDate = new Date(year + `/` + month + `/` + days).getDay();
    if (endDate == 0) {
        zhou = zhou - 1;
    }
    console.log(month + '月有' + zhou + '周')
    var secondDay = 0;
    switch (day) {
        case 7:secondDay = 1 + 1;break;
        case 1:secondDay = 1;break;
        case 2:secondDay = 1 + 5;break;
        case 3:secondDay = 1 + 4;break;
        case 4:secondDay = 1 + 3;break;
        case 5:secondDay = 1 + 2;break;
        case 6:secondDay = 1 + 1;break;
    }
    let timesArray = [];
    timesArray[0] = 1 + (7 - day);
    for (let i = 1; i < zhou - 1; i++) {
        if (i == 1) {
        timesArray[i] = timesArray[0] + 7;
        } else {
        timesArray[i] = timesArray[i - 1] + 7;
        }
    }
    timesArray[zhou - 1] = days;

    let timesArray2 = []
    timesArray2[0] = (7 - day);
    for (let i = 1; i < zhou - 1; i++) {
        if (i == 1) {
            timesArray2[i] = timesArray[0] + 6;
        } else {
            timesArray2[i] = timesArray[i - 1] + 6;
        }
    }
    var array = timesArray.map(function (item) {
        return month + `/` + item;
    });
    console.log(timesArray2.concat(timesArray))

    return timesArray2.concat(timesArray)
}


export const formatDate = (data, type = 'YYYY-MM-DD') => {
    let year = data.getFullYear(),
        month = data.getMonth() + 1,
        day = data.getDate(),
        hours = data.getHours(),
        minutes = data.getMinutes(),
        second = data.getSeconds(),
        result = '';

    switch (type) {
        case "YYYY":
            result = `${year}`;
            break;
        case "MM":
            result = `${format(month)}`;
            break;
        case "DD":
            result = `${format(day)}`;
            break;
        case "YYYY-MM":
            result = `${year}-${format(month)}`;
            break;
        case "MM-DD":
            result = `${format(month)}-${format(day)}`;
            break;
        case "hh:mm":
            result = `${format(hours)}:${format(minutes)}`;
            break;
        case "YYYY-MM-DD":
            result = `${year}-${format(month)}-${format(day)}`;
            break;
        case "YYYY-MM-DD hh:mm":
            result = `${year}-${format(month)}-${format(day)} ${format(hours)}:${format(minutes)}`;
            break;
        case "YYYY-MM-DD hh:mm:ss":
            result = `${year}-${format(month)}-${format(day)} ${format(hours)}:${format(minutes)}:${format(second)}`;
            break;
        case "M":
            result = `${month}`;
            break;
        case "D":
            result = `${day}`;
            break;
        case "YYYY-M":
            result = `${year}-${month}`;
            break;
        case "M-D":
            result = `${month}-${day}`;
            break;
        case "h:m":
            result = `${hours}:${minutes}`;
            break;
        case "YYYY-M-D":
            result = `${year}-${month}-${day}`;
            break;
        case "YYYY-M-D h:m":
            result = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        case "YYYY-M-D h:m:s":
            result = `${year}-${month}-${day} ${hours}:${minutes}:${second}`;
            break;
    }

    return result;
}

export const myOtherMonth=(sm,monthNum)=>{
    var time = sm || new Date();
    var year = time.getFullYear();
    var month = time.getMonth();
    var year2 = year;
    var month2 = parseInt(month) + monthNum;
    let mt = parseInt(Math.abs(month2) / 12)
    let my = Math.abs(month2) % 12
    let m = mt == 0 ? 1 : mt
    if(monthNum < 0 && month2 <=0){
        year2 = parseInt(year2) - m;
        month2 = 12 - my;
    }else if(monthNum > 0 && month2 >12){
        year2 = parseInt(year2) + m
        month2 = my
    }
    return year2 + '/' +format( month2);
}

export const getUpperCase = str => {
    return str.substr(0,1).toLocaleUpperCase() + str.substr(1)
}